//
//  TypeView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/8.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol TypeViewDelegate {
    @objc optional func selectType(title:String) //选择办卡类型
}

class TypeView: UIView {
    
    weak var typeDelegate : TypeViewDelegate?
    
    fileprivate let cellId = "typeCell"
    
    var dataArray = [String]()
    
    let headView = UIView()
    
    let titleBtn = UIButton()
    
    var collectionView : UICollectionView!
    
    var text : String! {
        
        didSet{
            self.titleBtn.setTitle(text, for: .normal)
        }
    }

    init() {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.black.withAlphaComponent(0.4)
        
        headView.backgroundColor = UIColor.white
        self.addSubview(headView)
        
        titleBtn.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        titleBtn.setTitleColor(UIColor.black, for: .normal)
        titleBtn.addTarget(self, action: #selector(selectType(sender:)), for: .touchUpInside)
        headView.addSubview(titleBtn)
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSize.init(width: ScreenW/3, height: RATIO_H(maxNum: 40))
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.isScrollEnabled = false
        collectionView.backgroundColor = UIColor.clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(TypeCell.self, forCellWithReuseIdentifier: cellId)
        headView.addSubview(collectionView)
        
        headView.snp.makeConstraints { (make) in
            make.top.left.right.bottom.equalToSuperview()
        }
        
        titleBtn.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.centerX.equalTo(ScreenW/6)
            make.width.equalTo(ScreenW/3)
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
        
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(titleBtn.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        
    }
    
    //MARK: 选择所有
    @objc fileprivate func selectType(sender:UIButton) {
        
        self.typeDelegate?.selectType!(title: (sender.titleLabel?.text)!)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension TypeView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? TypeCell
        cell?.titleLabel.text = dataArray[indexPath.row]
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        self.typeDelegate?.selectType!(title: dataArray[indexPath.row])
    }
    
}
